package cn.zhfeat.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;


/**
 * 
 * @Title JspToHtmlUtils.java
 * @Description 生成html工具类
 * @author wschenyongyin
 * @date 2017年11月17日
 * @version 1.0
 */
public class JspToHtmlUtils {

	/**
	 * 根据本地模板生成静态页面
	 * 
	 * @param modelHtmlPath
	 *            html模板地址
	 * @param saveHtmlPath
	 *            保存的html地址
	 * @return
	 */
	public static String JspToHtmlFile(String modelHtmlPath,
			String saveHtmlPath, Map<String, String> replaceContentMap) {
		String returnPath = null;
		String str = "";
		try {
			String tempStr = "";
			FileInputStream is = new FileInputStream(modelHtmlPath);// 读取模块文件
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			while ((tempStr = br.readLine()) != null)
				str = str + tempStr;
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		try {
			if (StringUtils.isEmpty(replaceContentMap.get("title"))) {
				str = str.replaceAll("###title###",
						replaceContentMap.get("title"));
			} else {
				str = str.replaceAll("###title###", "活动");
			}
			if (StringUtils.isEmpty(replaceContentMap.get("content"))) {
				str = str.replaceAll("###content###",
						replaceContentMap.get("content"));
			} else {
				str = str.replaceAll("###content###", "");
			}

			if (StringUtils.isEmpty(replaceContentMap.get("formContent"))) {
				str = str.replaceAll("###form###",
						replaceContentMap.get("formContent"));
			} else {
				str = str.replaceAll("###form###", "");
			}

			if (StringUtils.isEmpty(replaceContentMap.get("footerContent"))) {
				str = str.replaceAll("###footer###",
						replaceContentMap.get("footerContent"));
			} else {
				str = str.replaceAll("###footer###", "");
			}
			File f = new File(saveHtmlPath);
			if (!f.exists()) {
				try {
					f.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				BufferedWriter o = new BufferedWriter(new FileWriter(f, false));
				o.write(str);
				o.close();
			}
			returnPath = FileUtil.uploadFile(f);
			System.out.println("--->" + returnPath);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return returnPath;
	}

	/**
	 * 根据url生成静态页面
	 * 
	 * @param u
	 *            动态文件路经 如：[url]http://www.163.com/x.jsp[/url]
	 * @param path
	 *            文件存放路经如：x:\\abc\bbb.html
	 * @return
	 */
	public static boolean JspToHtmlByURL(String u, String path) {
		// 从utl中读取html存为str
		String str = "";
		try {
			URL url = new URL(u);
			URLConnection uc = url.openConnection();
			InputStream is = uc.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			while (br.ready()) {
				str += br.readLine() + "\n";

			}
			is.close();
			// 写入文件
			File f = new File(path);
			BufferedWriter o = new BufferedWriter(new FileWriter(f));
			o.write(str);
			o.close();
			str = "";
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 根据url生成静态页面
	 * 
	 * @param url
	 *            动态文件路经 如：[url]http://www.163.com/x.jsp[/url]
	 * @return d
	 */
	public static StringBuffer getHtmlTextByURL(String url) {
		// 从utl中读取html存为str
		StringBuffer sb = new StringBuffer();
		try {
			URL u = new URL(url);
			URLConnection uc = u.openConnection();
			InputStream is = uc.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			while (br.ready()) {
				sb.append(br.readLine() + "\n");
			}
			is.close();
			return sb;
		} catch (Exception e) {
			e.printStackTrace();
			return sb;
		}
	}

}
